package com.zking.web.lj;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.Information;
import com.zking.dao.domain.Post;
import com.zking.service.lj.PostServicelj;
import com.zking.utils.RedisUtil;

@Controller
public class LPostController {
	@Autowired
	PostServicelj psl; 
	
	@Autowired
	RedisUtil redisUtil;
	
	//根据字段查询所有职位
	@RequestMapping("lsetpost")
	public String lsetpost(String job_audit) {
		if (redisUtil.lGetListSize("lsetpost")==0) {
			List<Map<String, Object>> maps = psl.getPast(job_audit);
			for (Map<String, Object> map : maps) {
				redisUtil.lSet("lsetpost", JSONObject.toJSONString(map));
			}
			return "redirect:/lgetpost";
		}else {
			return "redirect:/lgetpost";
		}
	}
			//使用Redis分页查询
			@RequestMapping("/lgetpost")
			public String lgetpost(HttpServletRequest request) {
				String pnS = request.getParameter("pn");

				int pn = 0;
				int limit = 5;
				try{
					pn = Integer.parseInt(pnS);
				} catch (Exception e) {
					pn = 1;
				}

				if(pn < 1){
					pn = 1;
				}

				int total = (int)redisUtil.lGetListSize("lsetpost");
				int pages = total / limit;
				if(total % limit != 0){
					pages++;
				}

				if(pn > pages){
					pn = pages;
				}

				int start = (pn - 1)*limit;//第一个参数
				limit =start+5; 
				List<Object> information= redisUtil.lGet("lsetpost", start, limit-1);
				List<Post> lists = new ArrayList<Post>();
				for (Object object : information) {
					lists.add(JSONObject.parseObject(object.toString(), Post.class));
				}
				if (!lists.isEmpty()) {
					request.setAttribute("lists", lists);
					request.setAttribute("pn", pn);
					request.setAttribute("limit", limit);
					request.setAttribute("pages", pages);
					return "/WEB-INF/admin/post";
				}else {
					request.setAttribute("msg", "没有查询到该数据");
					return "/WEB-INF/admin/post";
				}
			}
			//根据id查询一条数据
			@RequestMapping("getpostone")
			public String getpostone(Integer id, HttpServletRequest request) {
				Post post = psl.getPastone(id);
				request.setAttribute("post", post);
				 return "/WEB-INF/admin/postupdate";
			}
			@RequestMapping("updatepostone")
			public String updatepostone(Integer id, String job_audit) {
				int i = psl.updatepost(id, job_audit);
				if (i==1) {
					return "redirect:/delpost";
				}else {
					return "redirect:/delpost";
				}

			}
			//删除redis中的数据
			@RequestMapping("delpost")
			public String delpost() {
				String job_audit = null;
				try {
					job_audit = URLEncoder.encode("未审核", "utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				redisUtil.del("lsetpost");
				return "redirect:/lsetpost?job_audit="+job_audit;
			}
			//查询所有职位
			@RequestMapping("lsetposts")
			public String lsetposts() {
				if (redisUtil.lGetListSize("lsetposts")==0) {
					List<Map<String, Object>> maps = psl.getPasts();
					for (Map<String, Object> map : maps) {
						redisUtil.lSet("lsetposts", JSONObject.toJSONString(map),300);
					}
					return "redirect:/lgetposts";
				}else {
					return "redirect:/lgetposts";
				}
			}
			//使用Redis分页查询所有职位
			@RequestMapping("/lgetposts")
			public String lgetposts(HttpServletRequest request) {
				String pnS = request.getParameter("pn");
				String lgetposts = "lgetposts";

				int pn = 0;
				int limit = 10;
				try{
					pn = Integer.parseInt(pnS);
				} catch (Exception e) {
					pn = 1;
				}

				if(pn < 1){
					pn = 1;
				}

				int total = (int)redisUtil.lGetListSize("lsetposts");
				int pages = total / limit;
				if(total % limit != 0){
					pages++;
				}

				if(pn > pages){
					pn = pages;
				}

				int start = (pn - 1)*limit;//第一个参数
				limit =start+10; 
				List<Object> information= redisUtil.lGet("lsetposts", start, limit-1);
				List<Post> lists = new ArrayList<Post>();
				for (Object object : information) {
					lists.add(JSONObject.parseObject(object.toString(), Post.class));
				}
				if (!lists.isEmpty()) {
					request.setAttribute("lists", lists);
					request.setAttribute("pn", pn);
					request.setAttribute("site", lgetposts);
					request.setAttribute("limit", limit);
					request.setAttribute("pages", pages);
					return "/WEB-INF/admin/postlist";
				}else {
					request.setAttribute("msg", "没有查询到该数据");
					return "/WEB-INF/admin/postlist";
				}
			}
			 //查询未审核、已通过、未通过的职业
			@RequestMapping("/lsetpostjob")
			public String lsetpostjob(String job_audit) {
				if (redisUtil.lGetListSize("lsetpostjob")==0) {
					List<Map<String, Object>> maps = psl.getPast(job_audit);
					for (Map<String, Object> map : maps) {
						redisUtil.lSet("lsetpostjob", JSONObject.toJSONString(map));
					}
					return "redirect:/lgetpostjob";
				}else {
					return "redirect:/lgetpostjob";
				}
			}
			//使用Redis分页查询
			@RequestMapping("/lgetpostjob")
			public String lgetpostjob(HttpServletRequest request) {
				String pnS = request.getParameter("pn");
				String lgetpostjob = "lgetpostjob";

				int pn = 0;
				int limit = 5;
				try{
					pn = Integer.parseInt(pnS);
				} catch (Exception e) {
					pn = 1;
				}

				if(pn < 1){
					pn = 1;
				}

				int total = (int)redisUtil.lGetListSize("lsetpostjob");
				int pages = total / limit;
				if(total % limit != 0){
					pages++;
				}

				if(pn > pages){
					pn = pages;
				}

				int start = (pn - 1)*limit;//第一个参数
				limit =start+5; 
				List<Object> information= redisUtil.lGet("lsetpostjob", start, limit-1);
				List<Post> lists = new ArrayList<Post>();
				for (Object object : information) {
					lists.add(JSONObject.parseObject(object.toString(), Post.class));
				}
				if (!lists.isEmpty()) {
					request.setAttribute("lists", lists);
					request.setAttribute("pn", pn);
					request.setAttribute("site", lgetpostjob);
					request.setAttribute("limit", limit);
					request.setAttribute("pages", pages);
					return "/WEB-INF/admin/postlist";
				}else {
					request.setAttribute("msg", "没有查询到该数据");
					return "/WEB-INF/admin/postlist";
				}
				
			}
			//删除redis中的数据
			@RequestMapping("delpostjob")
			public String delpostjob(String job_audit) {
				try {
					job_audit = URLEncoder.encode(job_audit, "utf-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				redisUtil.del("lsetpostjob");
				return "redirect:/lsetpostjob?job_audit="+job_audit;
				
			}
}
